package org.jackysoft.entity;

import java.util.Comparator;
import java.util.Map;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import org.jackysoft.report.visitor.ArmItemVisitor;
import org.jackysoft.util.StringUtils;

import com.google.common.collect.Maps;

/**
 * 军品指标项
 */
@Entity
public class ArmItem extends AbstractItem implements Comparable<ArmItem>,
		Cloneable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4673387205606512257L;
	private Department department;
	private boolean freshed = false;
	private Double monthPlan;

	/**
	 * 
	 */
	public ArmItem() {
		super();
	}

	public ArmItem(String code) {
		Product host = new Product();
		host.setCode(code);
		this.setHost(host);
	}

	public void accetp(ArmItemVisitor vistor) {
		vistor.visite(this);
	}

	/**
	 * 对比项目按照code
	 * */
	@Override
	public int compareTo(ArmItem o) {
		if (o == null || null == o.getHost() || null == getHost())
			return -1;
		String ocode = StringUtils.lowerCase(StringUtils.trim(o.getHost()
				.getCode()));
		String tcode = StringUtils.lowerCase(StringUtils.trim(getHost()
				.getCode()));
		return ocode.compareTo(tcode);
	}

	@Override
	public ArmItem clone() {
		ArmItem o = null;

		try {

			o = (ArmItem) super.clone();
			o.setMonthPlan(this.monthPlan);
			Map<String, DataItem> dis = Maps.newHashMap();
			o.setDataItems(dis);
			for (String key : this.getDataItems().keySet()) {
				DataItem di = this.getDataItems().get(key);
				DataItem cdi = di.clone();
				cdi.setId(0);
				o.getDataItems().put(key, cdi);
			}

		} catch (CloneNotSupportedException e) {

			return null;

		}

		return o;

	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	@ManyToOne
	public Department getDepartment() {
		return department;
	}

	@Transient
	public Double getMonthPlan() {
		return monthPlan;
	}

	public void setMonthPlan(Double monthPlan) {
		this.monthPlan = monthPlan;
	}

	public boolean isFreshed() {
		return freshed;
	}

	public void setFreshed(boolean freshed) {
		this.freshed = freshed;
	}

	public class TypedComparator implements Comparator<ArmItem> {

		@Override
		public int compare(ArmItem o1, ArmItem o2) {
			if (o1 == null && o2 == null) return 0;
			if (o1 == null && o2!=null) return -1;
			if (o1 != null && o2==null) return 1;
			if(o1.getHost()==null || o2.getHost()==null) throw new IllegalArgumentException("数据项的 host[Product]属性不能为空");
			int coderst = o1.compareTo(o2);
			int typerst = o1.getItemType().compareTo(o2.getItemType());
			if(coderst==0 && typerst==0) return 0;
			return coderst;
		}

	}
}
